//
//  SJNewFeatureViewController.swift
//  SJWeiboSwift
//
//  Created by app;e on 15/10/3.
//  Copyright © 2015年 world. All rights reserved.
//
//  新特性界面

import UIKit

private let reuseIdentifier = "Cell"

class SJNewFeatureViewController: UICollectionViewController {

    //布局方式
    let layout = UICollectionViewFlowLayout()
    
    init(){
        super.init(collectionViewLayout: layout)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置layout
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //设置size
        layout.itemSize = view.bounds.size
        //设置item之间的间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        //设置滚动方向
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置分页
        collectionView?.scrollEnabled = true
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Register cell classes
        self.collectionView!.registerClass(SJNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
    
    // MARK: UICollectionViewDataSource
    let imageCount = 4
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! SJNewFeatureCell
    
        cell.imageIndex = indexPath.item
        return cell
    }
    
    // MARK: 代理方法
    ///  collectionView 停止滚动的动画方法
    ///
    ///  :param: collectionView collectionView
    ///  :param: cell           之前显示的 cell
    ///  :param: indexPath      之前显示的 indexPath
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
     
        //.1 获取当前cell
        let indexpath = collectionView.indexPathsForVisibleItems().last!
        
        if indexpath.item == (imageCount-1) {
            let cell = collectionView.cellForItemAtIndexPath(indexpath) as! SJNewFeatureCell
            cell.showStartButton()
        }
    }
}




// MARK: - newFeatureCell
class SJNewFeatureCell: UICollectionViewCell{
    
    //图片索引
    var imageIndex = 0{
        //属性监听
        didSet{
            imageIcon.image = UIImage(named: "new_feature_\(imageIndex+1)")
            
            startButton.hidden = true
        }
    }
    
    ///开始按钮显示动画
    func showStartButton(){
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.userInteractionEnabled = false
        startButton.hidden = false
        
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 40, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
                self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                self.startButton.userInteractionEnabled = true
        }
    }
    
    ///懒加载图片
    lazy var imageIcon: UIImageView = {
        return UIImageView()
    }()
    
    ///懒加载按钮
    lazy var startButton: UIButton={
        let btn = UIButton()
        btn.frame = CGRectMake(0, 0, 105, 36)
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        //给开始按钮添加点击事件
        btn.addTarget(self, action: "startBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    ///按钮点击 触发方法
    // 开始体验,发送通知切换控制器
     func startBtnClick(){
        //object:为true 表示切换到mainTabBarVC
        NSNotificationCenter.defaultCenter().postNotificationName(SJSwitchRootVCNotification, object: true)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(imageIcon)
        addSubview(startButton)
        
        setupBtn()
    }

    /// MARK: -布局开始按钮
    private func setupBtn(){
    
        startButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem:self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        imageIcon.frame = self.bounds
    }
}
